﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.Script.Services;
using System.Web.Services;

namespace FaisalMoversWebService
{
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
     [System.Web.Script.Services.ScriptService]
    public class Service1 : System.Web.Services.WebService
    {
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public string FindBuses(String source, String destination , String date)
        {

            try
            {
                DateTime s = DateTime.Parse(date);
                String z,y;
                Faisal_MoversEntities fmedt = new Faisal_MoversEntities();
              
                var f = (from t in fmedt.Travels 
                         join c in fmedt.Routes on t.Route equals c
                         join b in fmedt.Buses  on t.Bus equals b
                        
                         where c.route_from == source && c.route_to == destination && t.travelDate == s
                         select new {t.travel_id, c.route_from, c.route_to, b.bus_seats, t.travelArrivalTime, t.travelDepatureTime, b.bus_type, t.price }).ToList();
                List<object> bussesList = new List<object>();
                
                foreach (var v in f)
                {
                    
                    List<object> seatsList = new List<object>();
                    var rrr = fmedt.Reservations.Where(r => (r.Travel.travel_id == v.travel_id) && (r.is_book == true)).Select(r =>r.seat_number).ToArray();
                    foreach(var reserv in rrr)
                    {
                        var array = reserv.ToString().Split(',');
                        seatsList.AddRange(array);
                    }

                    bussesList.Add(new { v.travel_id, v.route_from, v.route_to, v.bus_seats, v.travelArrivalTime, v.travelDepatureTime, v.bus_type, v.price, seatsList });
                }
                JavaScriptSerializer os = new JavaScriptSerializer();

                z = os.Serialize(bussesList);
              
                
                return z;
            }
            catch (Exception e)
            { 
                   string str=e.Message;
                   return e.Message;
            }
        
        }

        [WebMethod]
        //[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public string DeleteReservation(int travel_id, int customer_id)
        {
            
            try
            {
                Faisal_MoversEntities fmedt = new Faisal_MoversEntities();
                //var travel = fmedt.Travels.Where(t => t.travel_id == travel_id).FirstOrDefault();
                //var customer = fmedt.Customers.Where(c => c.customer_id == customer_id).FirstOrDefault();
                Reservation reservation = fmedt.Reservations.Where(v => (v.Travel.travel_id == travel_id) && (v.Customer.customer_id == customer_id)).FirstOrDefault();
                reservation.is_book = false;
                fmedt.SaveChanges();


                return "Success";
            }
            catch (Exception e)
            {
                string str = e.Message;
                return e.Message;
            }
        }
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public string ViewReservation(String user_name, String mobile_number)
        {

            try
            {
                String z, y;
                Faisal_MoversEntities fmedt = new Faisal_MoversEntities();

                var f = (from r in fmedt.Reservations
                         join t in fmedt.Travels on r.Travel equals t
                         join c in fmedt.Customers on r.Customer equals c
                         join ru in fmedt.Routes on t.Route equals ru
                         where c.user_name == user_name && c.mobile_number == mobile_number && r.is_book == true
                         select new {t.travel_id,ru.route_from, ru.route_to , t.travelDate, t.travelDepatureTime , t.travelArrivalTime  ,r.res_no_of_seats , r.seat_number }).ToList();
                JavaScriptSerializer os = new JavaScriptSerializer();

                z = os.Serialize(f);
                return z;
            }
            catch (Exception e)
            {
                string str = e.Message;
                return e.Message;
            }

        }

       
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public string signup(String full_name, String user_name, String password, string mobile_number,bool sex, string age, string email_id)
        {
           
            Faisal_MoversEntities efm = new Faisal_MoversEntities();
            Customer custm = new Customer()
            {
                full_name = full_name,
                user_name = user_name,
                password = password,
                mobile_number = mobile_number,
                age = age,
                email_id = email_id,
                gender=sex 
            };

            efm.AddToCustomers(custm);
            efm.SaveChanges();

            if (custm == null)
            {
                return "false";
            }

            return custm.customer_id.ToString();
      }


        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public String Reservation(int id, int travel_id ,int number_of_seat , String seat_number)
        {
            String str = "Successs";
            Faisal_MoversEntities efm = new Faisal_MoversEntities();

           Customer custom = efm.Customers.Where(c => c.customer_id == id).First();
           Travel travel = efm.Travels.Where(v => v.travel_id == travel_id).First();
            Reservation res = new Reservation()
            {
                res_no_of_seats = number_of_seat,
                seat_number= seat_number,
                res_date = System.DateTime.Now,
               Customer = custom,
               Travel = travel,
               is_book = true

            };

            efm.AddToReservations(res);
            efm.SaveChanges();
            return str;
        }

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public String Feedback(String feedback_type, String subject, String comment, String name, String email_id, String phn_number)
        {
            String message = "Successs";

            Faisal_MoversEntities ef = new Faisal_MoversEntities();
             
            Feedback fb = new Feedback()
            {
                feedback_type= feedback_type,
                subject=subject,
                comment=comment,
                name=name,
                email_id=email_id,
                phn_number=phn_number
            };

            ef.AddToFeedbacks(fb);
            ef.SaveChanges();
            return message;
        }

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public string login(String user_name, String password)
        {

            
            Faisal_MoversEntities edt = new Faisal_MoversEntities();
            Customer c = (from r in edt.Customers where r.user_name == user_name && r.password == password select r).FirstOrDefault();
            // JavaScriptSerializer os = new JavaScriptSerializer();
            //  z = os.Serialize(c);
            if (c == null)
                return "false";
            else
                return c.customer_id.ToString();

        }






 }

       
    }